/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.bookingmanager.adapters.impl;

import cz.muni.fi.pa165.bookingmanager.adapters.BookingAdapter;
import cz.muni.fi.pa165.bookingmanager.dto.CustomerDTO;
import cz.muni.fi.pa165.bookingmanager.dto.HotelDTO;
import cz.muni.fi.pa165.bookingmanager.dto.RoomDTO;
import cz.muni.fi.pa165.bookingmanager.entities.Customer;
import cz.muni.fi.pa165.bookingmanager.entities.Hotel;
import cz.muni.fi.pa165.bookingmanager.entities.Room;
import cz.muni.fi.pa165.bookingmanager.services.BookingService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Jiří Holuša
 */
@Stateless
public class BookingAdapterImpl implements BookingAdapter {

    @EJB
    private BookingService bookingService;

    @Override
    public List<RoomDTO> findAllFreeRooms(HotelDTO hotel, Date startTime, int duration) {
        Hotel hotelEntity = AdaptersHelper.hotelDTOToEntity(hotel);

        List<Room> roomsEntities = bookingService.findAllFreeRooms(hotelEntity, startTime, duration);

        List<RoomDTO> roomsDTO = new ArrayList<RoomDTO>();
        for (Room roomEntity : roomsEntities) {
            roomsDTO.add(AdaptersHelper.roomEntityToDTO(roomEntity));
        }

        return roomsDTO;
    }

    @Override
    public List<CustomerDTO> findAllCurrentCustomersInHotel(HotelDTO hotel, Date startTime, int duration) {
        Hotel hotelEntity = AdaptersHelper.hotelDTOToEntity(hotel);

        List<Customer> customersEntities = bookingService.findAllCurrentCustomersInHotel(hotelEntity, startTime, duration);

        List<CustomerDTO> customersDTO = new ArrayList<CustomerDTO>();
        for (Customer customerEntity : customersEntities) {
            customersDTO.add(AdaptersHelper.customerEntityToDTO(customerEntity));
        }

        return customersDTO;
    }

    @Override
    public List<HotelDTO> searchHotelByConditions(String city, BigDecimal maxPrice, int minCapacity) {
        List<Hotel> hotelsEntities = bookingService.searchHotelByConditions(city, maxPrice, minCapacity);

        List<HotelDTO> hotelsDTO = new ArrayList<HotelDTO>();
        for (Hotel hotelEntity : hotelsEntities) {
            hotelsDTO.add(AdaptersHelper.hotelEntityToDTO(hotelEntity));
        }

        return hotelsDTO;
    }

    @Override
    public List<RoomDTO> searchRoomInHotel(HotelDTO hotel, int capacity, BigDecimal maxPrice) {
        Hotel hotelEntity = AdaptersHelper.hotelDTOToEntity(hotel);

        List<Room> roomsEntities = bookingService.searchRoomInHotel(hotelEntity, capacity, maxPrice);

        List<RoomDTO> roomsDTO = new ArrayList<RoomDTO>();
        for (Room roomEntity : roomsEntities) {
            roomsDTO.add(AdaptersHelper.roomEntityToDTO(roomEntity));
        }

        return roomsDTO;
    }

    public void setBookingService(BookingService bookingService) {
        this.bookingService = bookingService;
    }
}
